package com.lan.schedule;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;
import android.os.Environment;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MyRemoteViewsFactory extends Activity implements RemoteViewsService.RemoteViewsFactory {
    private final Context mContext;
    public  List<String> mList = new ArrayList<>();
    public  List<String> mList1 = new ArrayList<>();
    public  List<String> mList2 = new ArrayList<>();
    public  List<String> mList3 = new ArrayList<>();
    public  List<String> mList4 = new ArrayList<>();
    public  List<String> mList5 = new ArrayList<>();
    public  List<String> mList6 = new ArrayList<>();
    public  List<String> mList7 = new ArrayList<>();
    List<String>[] ml= new List[]{ mList1, mList2, mList3,mList4,mList5,mList6,mList7};
    String name1[] = new String[50];
    String loc[] = new String[50];
    int week[][] = new int[50][30];
    int we[] = new int[50];
    int day[] = new int[50];
    int jie[] = new int[50];
    int j;
    private Timer mTimer;
    String str1[] = new String[36];
    Bitmap bw = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    Bitmap bitmap;
    GradientDrawable drawable1 = new GradientDrawable();
public void ffr(){
    try {
        FileInputStream fileInputStream = mContext.openFileInput("test.txt");
        FileChannel f1 = fileInputStream.getChannel();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
        j = Integer.parseInt(bufferedReader.readLine());
        for (int i = 1; i < j; i++) {
            name1[i] = bufferedReader.readLine();
            loc[i] = bufferedReader.readLine();
            jie[i] = Integer.parseInt(bufferedReader.readLine());
            day[i] = Integer.parseInt(bufferedReader.readLine());
            we[i] = Integer.parseInt(bufferedReader.readLine());
            for (int p = 1; p <= we[i]; p++) {
                week[i][p] = Integer.parseInt(bufferedReader.readLine());
            }
        }
        f1.close();
        bufferedReader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    long wee = 0;
    String date = null;
    try {
        String strFilePath = String.valueOf(Environment.getExternalStorageDirectory()) + "/Android/data/com.lan.schedule/file/th";
        File file = new File(strFilePath, "date.txt");//在创建文件
        file.canRead();
        InputStream ff = null;
        ff = new FileInputStream(file);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ff));
        date=bufferedReader.readLine();
        ff.close();
        bufferedReader.close();
//            ba = ba1;
//            sh = sh1;
//            fo = fo1;
    } catch (Exception e) {
        e.printStackTrace();
    }
    String eTime = date;
    long nd = 1000 * 24 * 60 * 60 * 7;// 一天的毫秒数
    long diff = 0;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date curDate = new Date(System.currentTimeMillis());
    String nowtime = simpleDateFormat.format(curDate);
    try {
        diff = simpleDateFormat.parse(nowtime).getTime() - simpleDateFormat.parse(eTime).getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    wee = diff / nd + 1;// 计算周数
    for (int i = 1; i < 36; i++) {
        int fl = 0;
        int ji;
        ji = i / 7 + 1;
        if (i % 7 == 0) ji = ji - 1;
        int da;
        da = i % 7;
//            if (da == 0) da = 5;
        outterLoop:
        for (int g = 1; g < j; g++) {

            if (day[g] == da && jie[g] == ji) {
                for (int p = 1; p <= we[g]; p++) {
                    if (wee == week[g][p]) {
                        fl = 1;
                        str1[i] = name1[g] + loc[g];
                        break outterLoop;
                    }
                }
            }
        }
        if (fl == 0) str1[i] = "";
    }
    for(int i=0;i<7;i++){
        ml[i].clear();
        for(int k=0;k<5;k++)
            ml[i].add(str1[k*7+i+1]);
    }
}
    /*
     * 构造函数
     */
    public MyRemoteViewsFactory(Context context, Intent intent) {

        mContext = context;
    }

    /*
     * MyRemoteViewsFactory调用时执行，这个方法执行时间超过20秒回报错。
     * 如果耗时长的任务应该在onDataSetChanged或者getViewAt中处理
     */
    @SuppressLint("WrongConstant")
    @Override
    public void onCreate() {
        drawable1.setShape(GradientDrawable.RECTANGLE);
        drawable1.setGradientType(GradientDrawable.RECTANGLE);
        drawable1.setCornerRadius(25);
//        drawable1.setStroke(10,0xff000000);
        drawable1.setColor(0xffffffff);
        bitmap = Bitmap.createBitmap(138, 482, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable1.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable1.draw(canvas);
        ffr();
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                ffr();
            }
        }, 0, 1000 * 10);
        System.out.println("7899877789");
        for (int i = 1; i < 6; i++) {
            mList.add(i+"");
        }


    }

    /*
     * 当调用notifyAppWidgetViewDataChanged方法时，触发这个方法
     * 例如：MyRemoteViewsFactory.notifyAppWidgetViewDataChanged();
     */
    @Override
    public void onDataSetChanged() {
System.out.println("8888888");
    }

    /*
     * 这个方法不用多说了把，这里写清理资源，释放内存的操作
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        mList.clear();
        for(int i=0;i<7;i++){
            ml[i].clear();
        }
    }

    /*
     * 返回集合视图数量
     */
    @Override
    public int getCount() {
        return mList.size();
    }

    /*
     * 创建并且填充，在指定索引位置显示的View，这个和BaseAdapter的getView类似
     */
    @Override
    public RemoteViews getViewAt(int position) {
        if (position < 0 || position >= mList.size())
            return null;
        String content = mList.get(position);
        String[] da=new String[7];
        for(int i=0;i<7;i++){
            da[i]=ml[i].get(position);
        }
        // 创建在当前索引位置要显示的View
        final RemoteViews rv = new RemoteViews(mContext.getPackageName(),
                R.layout.list_item);
int[] idt={R.id.d1,R.id.d2,R.id.d3,R.id.d4,R.id.d5,R.id.d6,R.id.d7};
        int[] idm={R.id.dm1,R.id.dm2,R.id.dm3,R.id.dm4,R.id.dm5,R.id.dm6,R.id.dm7};
        // 设置要显示的内容
        rv.setTextViewText(R.id.jie, content);
for(int i=0;i<7;i++){
    rv.setTextViewText(idt[i],da[i]);
    if(da[i].equals("")){
        rv.setImageViewBitmap(idm[i],bw);
    }else rv.setImageViewBitmap(idm[i],bitmap);
}
        System.out.println("ffffff");
        // 填充Intent，填充在AppWdigetProvider中创建的PendingIntent
        Intent intent = new Intent();
        // 传入点击行的数据
        intent.putExtra("content", content);
//        rv.setOnClickFillInIntent(R.id.item, intent);

        return rv;
    }

    /*
     * 显示一个"加载"View。返回null的时候将使用默认的View
     */
    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    /*
     * 不同View定义的数量。默认为1（本人一直在使用默认值）
     */
    @Override
    public int getViewTypeCount() {
        return 1;
    }

    /*
     * 返回当前索引的。
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /*
     * 如果每个项提供的ID是稳定的，即她们不会在运行时改变，就返回true（没用过。。。）
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }
}

